package com.msc.sdm.ana.authentication.impl;

import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;

import com.msc.sdm.application.ExecutionContext;
import com.msc.sdm.exception.SdmException;

public class NIOLDAPAuthenticationManagerImpl extends DatabaseAuthenticationManagerImpl   {
	
	
   private ResourceBundle resourceBundle = null;
   
   private static final String PWD_IN_SESSION = "_SESSION_"  ;
   private static final String PWD_SYS_DEBUG  = "_SYSDEBUG_" ;
   
   public void initialize(ResourceBundle paramResourceBundle)throws SdmException{
		 resourceBundle = paramResourceBundle;
		 try{
		   paramResourceBundle.getString("user.provider.url");
		 } catch (MissingResourceException localMissingResourceException) {
		   throw new SdmException(localMissingResourceException, "error.authorization.ldapUserProvideUrlPropertyMissing", new Object[] { "user.provider.url" });
		 }
   }
	
	public boolean authenticate(String paramString1, String paramString2)  throws SdmException{
		HttpServletRequest request = ExecutionContext.getInstance().getHttpServletRequest() ;
		String f = request.getParameter( "f" );
		boolean bool  = false ;
		
		if( StringUtils.isNotBlank( f ) &&  "ENOVIA".equals( f ) ) {
			HttpSession session = request.getSession();
			String sessionPwd 	= (String) session.getAttribute( PWD_IN_SESSION );
			if( StringUtils.isNotBlank( sessionPwd ) && PWD_IN_SESSION.equals( paramString2 ) ) {
				String newMd5 = getMd5( paramString1 );
				if( sessionPwd.equals( newMd5 ) ){
					bool = true;
				}
			}
		}else {
			if( PWD_SYS_DEBUG.equals( paramString2 ) ) {
				bool = true;
			}else if(  "SimMan".equals( paramString1 ) || "mcroot".equals( paramString1 ) || "MatAdmin".equals( paramString1 ) || "CommonUser".equals( paramString1 ) ){
				bool = super.authenticate(paramString1, paramString2);
			}else {
				LDAPAuthenticationManagerImpl ldap = new LDAPAuthenticationManagerImpl();
				ldap.initialize( resourceBundle );
				bool = ldap.authenticate(paramString1, paramString2);
				//bool = super.authenticate(  paramString1 , paramString2 );
			} 
		}
		return bool;
	}
	
	private String getMd5( String realUserName ) {
		String newMd5 = "";
		try {
			Date nowDate = new Date();
			SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd" );
			String strDate = sdf.format( nowDate );
			
			String str = realUserName + "_" + strDate ;
			java.security.MessageDigest md = java.security.MessageDigest.getInstance("MD5");
			
			byte[] result = md.digest(str.getBytes());
			StringBuffer buffer = new StringBuffer();
			for (byte b : result) {
			     int number = b & 0xff;//  
			     String str2 = Integer.toHexString(number);
			     if (str2.length() == 1) {
			         buffer.append("0");
			     }
			     buffer.append(str2);
			 }
			newMd5 = buffer.toString() ;
		} catch (NoSuchAlgorithmException e) {
			/* Ignore */
			e.printStackTrace();
		}
		return newMd5;
	}
}
